package com.star.vrserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.star.vrserver.api.R;
import com.star.vrserver.entity.Vaccine;
import com.star.vrserver.entity.query.VaccineQuery;
import com.star.vrserver.service.VaccineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author star
 * @since 2023-03-09
 */
@Api(tags = "疫苗信息Api")
@RestController
@RequestMapping("/vaccine")
public class VaccineController {
    @Autowired
    private VaccineService vaccineService;

    //获取所有疫苗信息
    @ApiOperation(value = "获取所有疫苗信息")
    @GetMapping("getAllVaccines")
    public R getVaccines() {
        return R.ok().data("VaccineLists", vaccineService.list());
    }

    //根据id查询疫苗信息
    @ApiOperation(value = "根据id查询疫苗信息")
    @GetMapping("getVaccine")
    public R getVaccine(@RequestParam String id) {
        return R.ok().data("vaccineInfo", vaccineService.getById(id));
    }

    //根据id删除疫苗
    @ApiOperation(value = "根据id删除疫苗")
    @DeleteMapping("deleteVaccine/{id}")
    public R deleteVaccine(@PathVariable String id) {
        //是否删除成功：逻辑删除
        boolean flag = vaccineService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    //添加疫苗
    @ApiOperation(value = "添加疫苗")
    @PostMapping("saveVaccine")
    public R saveVaccine(@RequestBody Vaccine vaccine) {
        boolean flag = vaccineService.save(vaccine);
        return flag ? R.ok() : R.error();
    }

    //分页查询疫苗  current 当前页  limit 每页记录数
    @ApiOperation(value = "分页展示疫苗列表")
    @GetMapping("pageVaccine/{current}/{limit}")
    public R pageListVaccine(@PathVariable long current,
                          @PathVariable long limit) {
        //创建page对象
        Page<Vaccine> vaccinePage = new Page<>(current, limit);
        //调用方法:底层封装，把分页所有数据封装到pageVaccine对象里面
        vaccineService.page(vaccinePage);
        //获取总记录数
        long total = vaccinePage.getTotal();
        //获取最终分页结果集
        List<Vaccine> records = vaccinePage.getRecords();
        //将结果组装返回前端
        return R.ok().data("total", total).data("records", records);
    }

    //条件查询带分页的方法
    @ApiOperation(value = "分页展示疫苗列表(带条件)")
    @PostMapping("pageVaccineCondition/{current}/{limit}")
    public R pageListVaccine(@PathVariable long current, @PathVariable long limit,
                          @RequestBody(required = false) VaccineQuery vaccineQuery) {
        //RequestBody表示前端使用json格式传数据，必须使用Post方式
        //创建page对象
        Page<Vaccine> pageVaccine = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Vaccine> wrapper = new QueryWrapper<>();
        // 多条件组合查询 mybatis学过:动态sql
        String name = vaccineQuery.getName();
        String beginDate = vaccineQuery.getBeginDate();
        //判断条件值是否为空，如果不为空拼接条件
        if (!"".equals(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!"".equals(beginDate)) {
            wrapper.ge("gmt_create", beginDate);
        }
        //添加排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        vaccineService.page(pageVaccine, wrapper);
        //总记录数
        long total = pageVaccine.getTotal();
        //数据list集合
        List<Vaccine> records = pageVaccine.getRecords();
        return R.ok().data("total", total).data("records", records);
    }

    //修改疫苗信息
    @ApiOperation(value = "修改疫苗信息")
    @PostMapping("updateVaccine")
    public R updateVaccine(@RequestBody Vaccine vaccine) {
        boolean flag = vaccineService.updateById(vaccine);
        return flag ? R.ok() : R.error();
    }
}
